# @FileName  :  student.py
# @Time      :  2025/7/29 11:04
# @Author    :  Ayanami
# @info      :  学生相关的Pydantic模型

from datetime import date, datetime
from typing import Optional
from pydantic import BaseModel, Field
from app.models.models import Gender, DictionaryType

class AreaResponse(BaseModel):
    id: int
    name: str = Field(..., description="地域名称")
    code: Optional[str] = Field(None, description="行政区划代码")

    class Config:
        from_attributes = True

# 字典响应
class DictionaryResponse(BaseModel):
    id: int
    type: DictionaryType = Field(..., description="字典类型")
    code: str = Field(..., description="字典编码")
    name: str = Field(..., description="字典名称")
    value: Optional[str] = Field(None, description="字典值")
    sort_order: int = Field(default=0, description="排序")
    parent_id: Optional[int] = Field(None, description="父级ID")
    is_active: bool = Field(default=True, description="是否启用")
    description: Optional[str] = Field(None, description="描述")

    class Config:
        from_attributes = True


# 简化的字典响应
class DictionarySimple(BaseModel):
    id: int
    name: str = Field(..., description="字典名称")
    code: str = Field(..., description="字典编码")
    value: Optional[str] = Field(None, description="字典值")

    class Config:
        from_attributes = True

# 简化的地区响应
class AreaSimple(BaseModel):
    id: int
    name: str = Field(..., description="地域名称")

    class Config:
        from_attributes = True

class StudentBase(BaseModel):
    name: str = Field(..., max_length=50, description="姓名")
    gender: Gender = Field(..., description="性别")
    phone: str = Field(..., max_length=20, description="手机号")
    birthday: Optional[date] = Field(None, description="出生日期")
    id_card: str = Field(..., max_length=18, description="身份证号")
    wechat: Optional[str] = Field(None, max_length=50, description="微信号")
    emergency_contact: str = Field(..., max_length=50, description="紧急联系人")
    emergency_phone: str = Field(..., max_length=20, description="紧急联系人电话")
    source_id: int = Field(..., description="来源ID")
    work_experience: Optional[str] = Field(None, description="工作经历")
    notes: Optional[str] = Field(None, description="备注")
    area_id: int = Field(..., description="地区ID")

    class Config:
        from_attributes = True


class StudentCreate(StudentBase):
    student_id: str = Field(..., max_length=20, description="学号")


class StudentUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=50, description="姓名")
    gender: Optional[Gender] = Field(None, description="性别")
    phone: Optional[str] = Field(None, max_length=20, description="手机号")
    birthday: Optional[date] = Field(None, description="出生日期")
    id_card: Optional[str] = Field(None, max_length=18, description="身份证号")
    is_verified: Optional[bool] = Field(None, description="实名认证状态")
    wechat: Optional[str] = Field(None, max_length=50, description="微信号")
    emergency_contact: Optional[str] = Field(None, max_length=50, description="紧急联系人")
    emergency_phone: Optional[str] = Field(None, max_length=20, description="紧急联系人电话")
    source_id: Optional[int] = Field(None, description="来源ID")
    work_experience: Optional[str] = Field(None, description="工作经历")
    notes: Optional[str] = Field(None, description="备注")
    area_id: Optional[int] = Field(None, description="地区ID")


class StudentResponse(StudentBase):
    id: int
    student_id: str = Field(..., description="学号")
    is_verified: bool = Field(default=False, description="实名认证状态")
    total_study_hours: int = Field(default=0, description="学习总时长")
    honors_count: int = Field(default=0, description="获得荣誉数量")
    source: DictionarySimple = Field(..., description="来源")
    # 地区信息
    area: AreaSimple = Field(..., description="地区")
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class StudentListResponse(BaseModel):
    total: int = Field(default=0, description="总记录数")
    items: list[StudentResponse] = Field(default_factory=list, description="学生列表")


class StudentDetailResponse(StudentResponse):
    pass